package com.cg.controller;

import com.cg.common.JWTUtils;
import com.cg.common.Result;
import com.cg.entity.Category;
import com.cg.entity.Event;
import com.cg.entity.StatisticsCircle;
import com.cg.service.CategoryService;
import com.cg.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/qualityManager/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private EventService eventService;
    //查询所有
    @RequestMapping("/circles")
    public Result<List<StatisticsCircle>> circle(@RequestHeader("Authorization") String token) {
        List<StatisticsCircle> statisticsCircles = categoryService.circle(JWTUtils.getAccount(token));
        return new Result<>(200, "查询成功", statisticsCircles);
    }
    //新增
    @RequestMapping("/insert")
    public Result<String> insert(@RequestBody Category category, @RequestHeader("Authorization") String token) {
        category.setCreatedBy(JWTUtils.getAccount(token));
        int a = categoryService.insert(category);
        System.out.println(a);
        if (a == 0) {
            return new Result<>(1, "分类名称已相同", "");
        }
        return new Result<>(200, "新增成功", "新增成功");


    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody Category category) {
        int a = categoryService.update(category);
        if (a == 0) {
            return new Result<>(1, "更新错误", "更新错误");
        }
        return new Result<>(200, "更新成功", "更新成功");
    }
    //查询所有
    @RequestMapping("/selectList")
    public Result<List<Category>> selectList(@RequestHeader("Authorization") String token) {
        List<Category> list = categoryService.selectList(JWTUtils.getAccount(token));
        return new Result<>(200, "查询成功", list);
    }

    //删除分类
    @RequestMapping("/deleteCategory")
    public Result<String> deleteCategory(@RequestBody Category category, @RequestHeader("Authorization") String token) {
        List<Event> list = eventService.selectA(category.getCategoryId(), JWTUtils.getAccount(token));
        if (list.size() != 0) {
            return new Result<>(2, "删除错误,该分类有关联事件", "删除错误,该分类有关联事件");
        }
        int a = categoryService.deleteOne(category.getCategoryId());
        if (a == 0) {
            return new Result<>(0, "删除错误", "删除错误");
        }
        return new Result<>(200, "删除成功", "删除成功");

    }

//    @PostMapping("/select")
//    public Result<List<Category>> select(@RequestBody User user){
//        System.out.println(111);
//        List<Category> category=categoryService.selectList(user.getAccount());
//        Result<List<Category>> result=new Result<>();
//        result.setCode(1);
//        result.setMessage("");
//        result.setData(category);
//        return result;
//    }


}

